import { app, shell, BrowserWindow, ipcMain,desktopCapturer,session } from 'electron'
import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
import { browserWidong,openChat ,openCollectionChat,sreenDesktopCapturer} from './ipc'

const fs = require('fs')
// const path = require('path');
console.log(process.versions.chrome)
console.log(process.versions.node)
console.log(process.versions.electron)

const login_width = 300
const login_height = 370
const reg_height = 490

function createWindow(): void {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: login_width,
    height: login_height,
    show: false,
    autoHideMenuBar: true,
    titleBarStyle: 'customButtonsOnHover',
    resizable: false,
    frame: true,
    transparent: false,
    ...(process.platform === 'linux' ? { icon } : {}),
    webPreferences: {
      preload: join(__dirname, '../preload/index.js'),
      sandbox: false,
      contextIsolation: false,
      // devTools: true,
      nodeIntegration:true,
      nodeIntegrationInWorker:true,
    }
  })
  mainWindow.webContents.openDevTools();
  mainWindow.on('ready-to-show', () => {
    mainWindow.show()
  })

  mainWindow.webContents.setWindowOpenHandler((details) => {
    shell.openExternal(details.url)
    return { action: 'deny' }
  })

  // HMR for renderer base on electron-vite cli.
  // Load the remote URL for development or the local html file for production.
  if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
    mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
  } else {
    mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
  }

  // ipcMain.on('reg', (_, isreg) => {
  //   console.log('reg', isreg)
  //   mainWindow.setResizable(true)
  //   if (isreg) {
  //     mainWindow.setSize(login_width,reg_height)
  //   }else{

  //     mainWindow.setSize(login_width,login_height)
  //   }
  // mainWindow.setResizable(false)

  // })
  // ipcMain.on('nvetoHome', () => {
  //   mainWindow.setResizable(true);
  //   mainWindow.setSize(850, 600);//居中显示
  //   mainWindow.center();//可以最大化
  //   mainWindow.setMaximizable(true);//设置最小亩口大小
  //   mainWindow.setMinimumSize(860,688);
  // })
  browserWidong((isreg) => {
    console.log('ssssss',isreg);
    
    mainWindow.setResizable(true)
    if (isreg) {
      mainWindow.setSize(login_width, reg_height)
    } else {
      mainWindow.setSize(login_width, login_height)
    }
    mainWindow.setResizable(false)
  })

  openChat((val) => {
    console.log('openChat',val);
    
      mainWindow.setResizable(true);
      mainWindow.setSize(850, 600);//居中显示
      mainWindow.center();//可以最大化
      mainWindow.setMaximizable(true);//设置最小窗口大小
      mainWindow.setMinimumSize(860,523);
  })
  openCollectionChat(() => {
     console.log('openCollectionChat','这是弹窗');
     const newWin = new BrowserWindow(
      {
        title:'聊天文件'
      }
     );

      newWin.setSize(850, 600);//居中显示
      newWin.center();//可以最大化
      newWin.setMaximizable(true);//设置最小窗口大小
      newWin.setMinimumSize(860,523);
      // newWin.once('ready-to-show', () => {       
      //   newWin.show()
      // })
      newWin.loadURL('http://localhost:8081/#/collection')
  })

  sreenDesktopCapturer((val:any)=>{
    console.log('截图截图',val);

      const mainWindow = new BrowserWindow()
    
      session.defaultSession.setDisplayMediaRequestHandler((request, callback) => {
        desktopCapturer.getSources({ types: ['screen'] }).then((sources) => {
          console.log(sources);
          
          callback({ video: sources[0], audio: 'loopback' })
        })

      })
    
      // mainWindow.loadFile('https://www.baidu.com')
 
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
  // Set app user model id for windows
  electronApp.setAppUserModelId('com.electron')

  // Default open or close DevTools by F12 in development
  // and ignore CommandOrControl + R in production.
  // see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
  app.on('browser-window-created', (_, window) => {
    optimizer.watchWindowShortcuts(window)
  })

  // IPC test
  ipcMain.on('ping', () => console.log('pong'))

  ipcMain.on('saveFile', (_, d) => {
    fs.writeFileSync('output.txt', d, 'utf8')
    console.log('写入文件完成')
  })
  ipcMain.handle('read-file', () => {
    console.log('读文件…………')
    const result = fs.readFileSync('output.txt', 'utf8')
    console.log(result)
    return result.toString()
  })

 


  
  createWindow()

  app.on('activate', function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

// In this file you can include the rest of your app"s specific main process
// code. You can also put them in separate files and require them here.
